package cn.jd.product.service.impl;

import cn.jd.basic.config.BussinessException;
import cn.jd.basic.util.BaiduAuditUtils;
import cn.jd.org.domain.Employee;
import cn.jd.org.domain.Shop;
import cn.jd.org.mapper.ShopMapper;
import cn.jd.pet.domain.Pet;
import cn.jd.pet.domain.PetOnlineAuditLog;
import cn.jd.pet.domain.SearchMasterMsg;
import cn.jd.pet.mapper.SearchMasterMsgMapper;
import cn.jd.product.domain.OnTheShelvesLog;
import cn.jd.product.domain.Product;
import cn.jd.product.domain.ProductDetail;
import cn.jd.product.domain.ProductOnlineAuditLog;
import cn.jd.product.mapper.OnTheShelvesLogMapper;
import cn.jd.product.mapper.ProductDetailMapper;
import cn.jd.product.mapper.ProductMapper;
import cn.jd.product.mapper.ProductOnlineAuditLogMapper;
import cn.jd.product.service.IProductService;
import cn.jd.basic.service.impl.BasicServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jd
 * @since 2022-10-31
 */
@Service
public class ProductServiceImpl extends BasicServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;
    @Autowired
    private ProductDetailMapper mapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OnTheShelvesLogMapper onTheShelvesLogMapper;
    @Override
    public void save(Product product) {
        product.setState(0L);
        super.save(product);
        ProductDetail productDetail = new ProductDetail();
        product.getProductDetail().setProductId(product.getId());
        mapper.save(product.getProductDetail());
    }

    /**
     * 上架
     * @param product
     */
    @Override
    public void update(Product product) {
        product.setCreatetime(new Date());
        super.update(product);
        product.getProductDetail().setProductId(product.getId());
        mapper.updateByProductId(product.getProductDetail());
    }

    @Override
    public void onsale(List<Long> ids,Employee employee) {
        Map<Long, Product> productMap = productMapper.findAll().stream().filter(pro -> pro.getState() == 0).collect(Collectors.toMap(Product::getId, pro -> pro));
        StringBuilder builder = new StringBuilder();
        Date date = new Date();
        for (Long id : ids) {
            Product product = productMap.get(id);
            if(!BaiduAuditUtils.TextCensor(product.getName())){
                builder.append("百度审核名字不通过"+product.getResources()) ;
            }
            if(!BaiduAuditUtils.ImgCensor(product.getResources().split(",")[0])){
                builder.append("百度审核图片不通过"+product.getResources()) ;
            }
            if(builder.length()>0){
                // 添加失败日志
                ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
                productOnlineAuditLog.setAuditTime(date);
                productOnlineAuditLog.setProductId(id);
                productOnlineAuditLog.setState(0);
                productOnlineAuditLog.setNote(builder.toString());
                productOnlineAuditLogMapper.save(productOnlineAuditLog);
                throw new BussinessException(builder.toString());


            }
            product.setState(1L);
            product.setOnsaletime(date);
            productMapper.update(product);

        }
        ids.forEach(id->{
            Product product = productMapper.findById(id);
            OnTheShelvesLog onTheShelvesLog = new OnTheShelvesLog();
            onTheShelvesLog.setEmpid(employee.getId());
            onTheShelvesLog.setName(product.getName());
            onTheShelvesLog.setProid(product.getId());
            onTheShelvesLogMapper.save(onTheShelvesLog);
        });
    }

    /**
     *
     * @param ids
     */
    @Override
    public void offsale(List<Long> ids,Employee employee) {
        productMapper.offsale(ids);
        // 保存日志
        ids.forEach(id->{
            Product product = productMapper.findById(id);
            OnTheShelvesLog onTheShelvesLog = new OnTheShelvesLog();
            onTheShelvesLog.setEmpid(employee.getId());
            onTheShelvesLog.setName(product.getName());
            onTheShelvesLog.setProid(product.getId());
            onTheShelvesLogMapper.save(onTheShelvesLog);
        });
    }

    @Override
    public Product getProductDetail(Long id) {
        return productMapper.getProductDetail(id);
    }

    /**
     * 下单
     * @param id
     * @param employee
     */
    @Override
    public void purchase(Long id, Employee employee) {
        // 查询订单是否存在
        Product product = productMapper.findById(id);
        if(product==null){
            throw new BussinessException("该订单不存在");
        }
        // 添加订单
        SearchMasterMsg searchMasterMsg = new SearchMasterMsg();
        searchMasterMsg.setName(product.getName());
        searchMasterMsg.setTitle("宠物美容");
        searchMasterMsg.setPrice(product.getSaleprice().doubleValue());
        Shop shop = shopMapper.findById(product.getShopid());
        searchMasterMsg.setShopId(product.getShopid());
        searchMasterMsg.setState(1);

        searchMasterMsg.setAddress(shop.getAddress());
        searchMasterMsgMapper.save(searchMasterMsg);


    }
}
